var proof = require('../../../../utils/proof.js');
var app = getApp();
var constract = require('../../../../utils/constract.js');

//回退的步数
var delta;

Page({
  data: {
    disabled: false
  },
  onLoad: function (options) {
    var that = this;
    var id = options.id || '';
    that.setData({
      id:id
    })
  },
  onShow:function(){
    var that=this;
    var id=that.data.id;
    
    app.Verification(function (token) {
      if (id) {
        delta = 2;
        wx.request({
          url: constract.invoiceDetai,
          header: { token: token },
          data: { id: id },
          success: function (res) {
            that.setData({
              detail: res.data.data
            })
          }
        });
      } else {
        delta = 1;
      }
    })
  },
  formSubmit: function(e){
    var value = e.detail.value;
    var name = value.name;
    var taxNum = value.taxNum;
    var address = value.address;
    var phone = value.phone;
    var bank = value.bank;
    var bankNum = value.bankNum;
    var that = this;
    //点击时禁用按钮
    that.setData({
      disabled: true
    })

    if (proof.trim(name) == '' || name.length > 50 ) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的抬头',
        showCancel: false
      });
      that.setData({
        disabled: false
      })
      return false;
    }
    if (proof.trim(phone) !== '' || phone.length > 20) {
      if (!proof.isTel(phone)) {
        wx.showModal({
          title: '提示',
          content: '请输入正确的电话号码',
          showCancel: false
        });
        that.setData({
          disabled: false
        })
        return false;
      }
    };
    if (proof.trim(bankNum) == !'') {
      if (!proof.isBank(bankNum)) {
        wx.showModal({
          title: '提示',
          content: '请输入正确的银行卡号',
          showCancel: false
        });
        that.setData({
          disabled: false
        })
        return false;
      }
    }

    if (wx.showLoading) {
      wx.showLoading({
        title: '保存中',
        mask: true,
      });
    }

    app.Verification(function (token) {
      wx.request({
        url: constract.invoicAddorEdit,
        header: { token: token },
        data: value,
        success: function (res) {
          if (wx.hideLoading) {
            wx.hideLoading();
          }
          // that.setData({
          //   detail: res.data.data,
          // })
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            mask: true,
            duration: 1000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: delta
            });
          }, 1000)
        },
        fail: function() {
          wx.showToast({
            title: '保存失败',
            mask: true,
            duration: 1000
          })
        }
      });
    })
  }
})